#include <stdio.h>	// printf()
#include <conio.h>	// _getch(), _kbhit()
#include <windows.h>// Sleep(), SetConsoleCursorPosition(), SetConsoleTextAttribute
#include <iostream>
using namespace std;

struct Vector2D
{
	int x, y;
	Vector2D(const int & x, const int & y)
		:x(x),y(y){}
	bool isAt(const int & x, const int & y)
	{
		return this->x == x && this->y == y;
	}
};

void main()
{
	SetConsoleTextAttribute(
		GetStdHandle(STD_OUTPUT_HANDLE),
		FOREGROUND_GREEN | FOREGROUND_INTENSITY);
	COORD start = {0, 0};
	SetConsoleCursorPosition(
		GetStdHandle(STD_OUTPUT_HANDLE),
		start);
	printf("Hello World\n");

	Vector2D player(4, 5);
	//second player vector
	Vector2D player2(3,4);
	//Vector2D player2(4, 5);
	char icon;
	bool running = true;
	char input,input2;
	const int width = 15, height = 10;

	// main game loop
	while(running)
	{
		// draw
		// draws the background
		SetConsoleCursorPosition(
			GetStdHandle(STD_OUTPUT_HANDLE),
			start);
		for(int r = 0; r < height; ++r)
		{
			for(int c = 0; c < width; ++c)
			{
				putchar('.');
			}
			putchar('\n');
		}
		putchar('\n');
		// draws the player
		COORD loc = {player.x, player.y};
		SetConsoleCursorPosition(
			GetStdHandle(STD_OUTPUT_HANDLE),
			loc);
		putchar(1);

		COORD loc2 = {player2.x++, player2.y--};
		SetConsoleCursorPosition(
			GetStdHandle(STD_OUTPUT_HANDLE),
			loc2);
		putchar(2);

		// input
		input = getch();
		input2 = getch();



	// -------------------------------------------------------------------------------------------------------
	// Known glitch, the two players do not control correctly, the movement does not follow their instructions
	// -------------------------------------------------------------------------------------------------------

		switch(input)
		{
		case 'w':	player.y--;	if(player.y<=-1)player.y++;break;
		case 'a':	player.x--;	if(player.x<=-1)player.x++;break;
		case 's':	player.y++; if(player.y>=height)player.y--;break;
		case 'd':	player.x++;	if(player.x>=width)player.x--;break;
		case 'q':	running = false;	break;
		}
		//adds controls for second player
				//// input
				//input2 = getch();
				//// update
				// does the same thing as the other tester
				// currently doesnt work either
				//while(input!='i'||input!='j'||input!='k'||input!='l'||input!='u')
				//input2 = getch();
				//switch(input2)
				//{
				//case 'i':	player2.y--;	if(player2.y<=-1)player.y++;break;
				//case 'j':	player2.x--;	if(player2.x<=-1)player.x++;break;
				//case 'k':	player2.y++;	if(player2.y>=height)player.y--;break;
				//case 'l':	player2.x++;	if(player2.x>=width)player.x--;break;
				//case 'u':	running = false;	break;
				//}
		switch(input2)
		{
		case 'i':	player2.y--;	if(player2.y<=-1)player2.y++;break;
		case 'j':	player2.x--;	if(player2.x<=-1)player2.x++;break;
		case 'k':	player2.y++;    if(player2.y>=height)player2.y--;break;
		case 'l':	player2.x++;	if(player2.x>=width)player2.x--;break;
		}
	}
	
}